﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUL;
using Entity;
using Common;


namespace PM.Admin.ctrUser
{
    public partial class ctrGroupNews : System.Web.UI.UserControl
    {
        private GroupNewsBul bul = new GroupNewsBul();
		private string Level = "";
        private string Lang = "vi";
        private static string Id = "";
        private void LoadData()
        {
            GridView1.DataSource = bul.GroupNews_GetByAll("vi");
            GridView1.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                ViewState["flag"] = "add";
            }
        }

        protected void ChkAll(object sender, EventArgs e)
        {
            var chkh = (CheckBox) GridView1.HeaderRow.FindControl("cbAll");
            foreach (GridViewRow row in GridView1.Rows)
            {
                var chk = (CheckBox) row.FindControl("cbitem");
                chk.Checked = chkh.Checked;
            }
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            LoadData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            LoadData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (GridView1.DataKeys[e.RowIndex] == null)
                return;
            var id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            bul.GroupNews_Delete(id);
            LoadData();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[4].Attributes.Add("Onclick", "return confirm('Bạn xóa dữ liệu này?')");
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string _id = Convert.ToString(e.CommandArgument);
            if (e.CommandName == "select")
            {
                Id = _id;
                List<GroupNews> list = bul.GroupNews_GetById(_id);
                txtName.Text = list[0].Name;
                txtTitle.Text = list[0].Title;
                txtOrd.Text = list[0].Ord;
                txtKeyword.Text = list[0].Keyword;
                txtDescription.Text = list[0].Description;
                rdbUt.SelectedValue = list[0].Index;
                cbActive.Checked = list[0].Active == "1" || list[0].Active == "true";
            }
            ViewState["flag"] = "edit";

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                GroupNews obj = new GroupNews();
                obj.Id = Id;
                obj.Index = rdbUt.SelectedValue;
                obj.Name = txtName.Text;
                obj.Description = txtDescription.Text;
                obj.Active = cbActive.Checked?"1":"0";
                obj.Level = Level;
                obj.Lang =Lang ;
                obj.Title = txtTitle.Text;
                obj.Keyword = txtKeyword.Text;
                obj.Ord = txtOrd.Text;
                obj.Tag = Common.StringClass.NameToTag(txtName.Text);
                if ( ViewState["flag"].Equals("add"))
                {
                    bul.GroupNews_Insert(obj);
                }
                else if (ViewState["flag"].Equals("edit"))
                {
                    bul.GroupNews_Update(obj);
                }
                ViewState["flag"] = "add";
                LoadData();
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            PageHelper.ResetControlValues(this);
            ViewState["flag"] = "add";
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            int sodong = GridView1.Rows.Count;
            for (int i = sodong-1 ; i >-1; i--)
            {
                string id = GridView1.DataKeys[i].Value.ToString();
                CheckBox ck = (CheckBox)GridView1.Rows[i].FindControl("cbitem");
                if (ck.Checked )
                {
                    bul.GroupNews_Delete(id);
                    LoadData();
                }
                else
                {
                    return;
                }
            }
            LoadData();
            
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string name = txtSearch.Text;
            string active = txtSearch.Text;
            GridView1.DataSource = bul.GroupNews_Search(name, active);
            GridView1.DataBind();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            LoadData();
            //PageHelper.ResetControlValues(this);
        }
        
    }
}